package com.lys.controller;

import com.lys.entity.Product;
import com.lys.entity.User;
import com.lys.service.ProductService;
import com.lys.utils.EmptyUtils;
import com.lys.utils.MemcachedUtils;
import com.lys.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/30 8:20
 */
@Controller
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private ProductService productService;

    /**
     * 跳转到历史记录
     * @return
     */
    @GetMapping("/list")
    public String toFavoriteList(HttpSession session, Model model) {
        List<Product> recentProducts= null;
        try {
            recentProducts = queryFavoriteList(session);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("recentProducts",recentProducts);
        return "/pre/product/favoriteList";
    }

    /**
     * 添加到收藏
     *
     * @return
     */
    @GetMapping("/save")
    @ResponseBody
    public ReturnResult addFavorite(HttpSession session,
                                    @RequestParam("id") String id) {
        ReturnResult result = new ReturnResult();
        // String id = request.getParameter("id");
        Product product = productService.getProductById(Integer.valueOf(id));
        List<Product> favoriteList = null;
        try {
            favoriteList = queryFavoriteList(session);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //判断是否满了
        if (favoriteList.size() > 0 && favoriteList.size() == 5) {
            favoriteList.remove(0);
        }
        boolean temp = false;
        for (int i = 0; i < favoriteList.size(); i++) {
            if (favoriteList.get(i).getId().equals(product.getId())) {
                temp = true;
                break;
            }
        }
        if (!temp) {
            favoriteList.add(favoriteList.size(), product);
        }
        //MemcachedUtils.add(getFavoriteKey(session),favoriteList);
        session.setAttribute("recentProducts", favoriteList);
        result.returnSuccess();
        return result;
    }

    /**
     * 查询最近商品
     * @return
     */
    private List<Product> queryFavoriteList(HttpSession session) {
        // HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        //判断用户是否登录
        //String key = EmptyUtils.isEmpty(user) ? session.getId() : user.getLoginName();
        //List<Product> recentProducts = (List<Product>) MemcachedUtils.get(key);
        List<Product> recentProducts = (List<Product>) session.getAttribute("recentProducts");
        if (EmptyUtils.isEmpty(recentProducts)) {
            recentProducts = new ArrayList<Product>();
        }
        return recentProducts;
    }
    /**
     *
     * @param session
     * @return
     */
//    private String getFavoriteKey(HttpSession session){
//        // HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("loginUser");
//        return EmptyUtils.isEmpty(user) ? session.getId() : user.getLoginName();
//    }

}
